package cn.stylefeng.guns.modular.cms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cms.entity.Law;
import cn.stylefeng.guns.modular.cms.mapper.LawMapper;
import cn.stylefeng.guns.modular.cms.model.params.LawParam;
import cn.stylefeng.guns.modular.cms.model.result.LawResult;
import  cn.stylefeng.guns.modular.cms.service.LawService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 技能知识 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-05-17
 */
@Service
public class LawServiceImpl extends ServiceImpl<LawMapper, Law> implements LawService {

    @Override
    public void add(LawParam param){
        Law entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(LawParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(LawParam param){
        Law oldEntity = getOldEntity(param);
        Law newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public LawResult findBySpec(LawParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<LawResult> findListBySpec(LawParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(LawParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(LawParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Law getOldEntity(LawParam param) {
        return this.getById(getKey(param));
    }

    private Law getEntity(LawParam param) {
        Law entity = new Law();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
